package com.bookdresser.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bookdresser.R;
import com.bookdresser.adapters.BooksAdapter;
import com.bookdresser.model.BookItem;
import com.bookdresser.utils.BooksDataDeal;

public final class ListFragment extends Fragment {

	private Context context;

	public static ListFragment newInstance(Context context) {
		ListFragment fragment = new ListFragment();
		fragment.context = context;
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tab_list, null);

		BooksDataDeal pullBookData = new BooksDataDeal();
		List<BookItem> books = new ArrayList<BookItem>();
		books = pullBookData.getBooksData();

		ListView lv_list = (ListView) view.findViewById(R.id.lv_list);
		BooksAdapter adapter = new BooksAdapter(context, books);
		lv_list.setAdapter(adapter);
		lv_list.setOnItemClickListener(new ListClickLitener(books, context));

		return view;
	}

	private class ListClickLitener implements OnItemClickListener {

		private Context context;
		private List<BookItem> books;

		public ListClickLitener(List<BookItem> books, Context context) {
			this.books = books;
			this.context = context;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long arg3) {

			Dialog dialog = new Dialog(context);
			View contentView = LayoutInflater.from(context).inflate(
					R.layout.book_info_detail, null);
			if (books != null) {
				final BookItem book = books.get(position);

				TextView tv_book_name = (TextView) contentView
						.findViewById(R.id.tv_book_name);
				TextView tv_editor = (TextView) contentView
						.findViewById(R.id.tv_editor);
				TextView tv_publishing = (TextView) contentView
						.findViewById(R.id.tv_publishing);
				TextView tv_linkman = (TextView) contentView
						.findViewById(R.id.tv_linkman);
				ImageView iv_book_image = (ImageView) contentView
						.findViewById(R.id.iv_book_image);
				Button bt_connect = (Button) contentView
						.findViewById(R.id.bt_connect);
				Button bt_collect_book = (Button) contentView
						.findViewById(R.id.bt_collect_book);

				tv_book_name.setText(book.getName());
				tv_publishing.setText(book.getPublishingHouse());
				tv_linkman.setText(book.getLinkmanName());
				iv_book_image.setImageDrawable(book.getIcon());

				bt_connect.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						final AlertDialog.Builder builder = new Builder(context);
						builder.setTitle("提醒");
						builder.setMessage("是否确定拨号？");
						builder.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										Intent intent = new Intent();
										intent.setAction(Intent.ACTION_CALL);
										intent.setData(Uri.parse("tel:"
												+ book.getLinkmanNumber()));

										startActivity(intent);
									}
								});
						builder.setNegativeButton("取消", new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								builder.create().dismiss();
							}
						});
						builder.create().show();

					}

				});

				bt_collect_book.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Toast.makeText(context, "收藏成功", Toast.LENGTH_LONG)
								.show();
					}
				});
			}

			WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
			int[] wh = getDeviceWH(context);
			windowParams.x = 0;
			windowParams.y = wh[1];

			dialog = getDialog(context, contentView, dialog, windowParams);
			dialog.show();
		}
	}

	public static Dialog getDialog(Context context, View contentView, Dialog d,
			LayoutParams windowParams) {
		// 防止dialog重复弹出
		if (d != null && d.isShowing()) {
			return d;
		}
		Dialog dialog = new Dialog(context, R.style.book_detail_dialog);
		Window window = dialog.getWindow();
		// 控制dialog停放位置
		window.setAttributes(windowParams);
		// window.setBackgroundDrawableResource(R.drawable.alert_dialog_background);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(contentView);
		// 最终决定dialog的大小,实际由contentView确定了
		dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		dialog.show();
		return dialog;
	}

	public static int[] getDeviceWH(Context context) {
		int[] wh = new int[2];
		int w = 0;
		int h = 0;
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		w = dm.widthPixels;
		h = dm.heightPixels;
		wh[0] = w;
		wh[1] = h;
		return wh;
	}
}
